<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Website_Sms extends CI_Controller {
	public function index(){
		if($this->session->userdata('website_id') == '' || $this->session->userdata('website_user') == ''){
			$this->load->helper(array('url'));
			redirect('/website/login', 'location', 301);
		}else{
			#$this->session->userdata('website_id').'|'.$this->session->userdata('website_user');
			$this->layout->setLayout('default');
			$this->layout->setTitle('Website | Report');			
			$web = $this->session->userdata('sms_code');
			if(isset($web)){			
				$pos = strpos($web,' ');
				if ($pos === false) {			
					$filter = " AND sms_code = '$web'";
				}else{
					$syn = substr($web, 0, $pos);
					$code = substr($web, $pos + 1, strlen($web));
					$filter = " AND sms_syntax = '$syn' AND sms_code = '$code'";
				}
			}
			$wid = intval($this->uri->segment(1));
			$page = $this->uri->segment(3) > 1 ? $this->uri->segment(3) : 1;
			if ($page > 1) $page = $page / PAGE_ITEM + 1;
			$offset = ($page - 1) * PAGE_ITEM;
			$month = isset($_GET['month']) ? date('Y') ."-" . quotes_to_entities($_GET['month']) : date('Y-m');
			
			$filter .= " AND receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
			$filmonth =  " AND T.message_request = '$web' AND T.receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
					
			
			//---------------- LIST SMS --------------------						
			$field = "Count(T.phone) As SoLuong";
			if ($web == "GN mkh198542776") $field = "Round(Count(T.phone)*0.7,0) As SoLuong";
			$sqlwebsite = "SELECT SQL_CALC_FOUND_ROWS @row := @row + 1 AS STT, DATE_FORMAT(T.receive_date, '%d-%m-%Y') AS earn_date, T.telco, T.service_num, T.syntax,  T.Status, $field
FROM `tdx_sms_mo` T, (Select @row := 0) R
WHERE  1=1 $filmonth
Group by earn_date, telco, service_num, syntax, message_request
Order By id, service_num, telco LIMIT $offset, ".PAGE_ITEM;
			$querywebsite = $this->db->query($sqlwebsite);
			// get total
			$sqlTotal = "SELECT FOUND_ROWS() AS found_row";
			$queryTotal = $this->db->query($sqlTotal);
			$dataTotal = $queryTotal->row();
			if($dataTotal->found_row > 0){				
				if($dataTotal->found_row > PAGE_ITEM){
					$this->load->library('pagination');
					$config['base_url'] = $this->config->item('base_url').'/website_sms/index/';
					$config['total_rows'] = $dataTotal->found_row;
					$config['per_page'] = PAGE_ITEM;
					$config['num_tag_open'] = '<li>';
					$config['num_tag_close'] = '</li>';
					$config['cur_tag_open'] = '<li><span>';
					$config['cur_tag_close'] = '</span></li>';
					$config['prev_link'] = 'Previous';
					$config['prev_tag_open'] = '<li>';
					$config['prev_tag_close'] = '</li>';
					$config['next_link'] = 'Next';
					$config['next_tag_open'] = '<li>';
					$config['next_tag_close'] = '</li>';
					$config['uri_segment'] = 3;
					$config['first_tag_open'] = '<li>';
					$config['first_tag_close'] = '</li>';
					$config['last_tag_open'] = '<li>';
					$config['last_tag_close'] = '</li>';
					
					$this->pagination->initialize($config);
					$data['paginate'] = $this->pagination->create_links();
				}
				$data['total'] = $dataTotal->found_row;
				if ($querywebsite->num_rows() > 0){
					foreach ($querywebsite->result_array() as $row){
						$data['items'][] = $row;
					}
				}
			}
						
			//----------------------
			$arrDate = explode("-", $month);
			$data['month'] = $arrDate[1].'-'.$arrDate[0];
			// get earning data
			$sqlThisMonthEarning = "SELECT SUM(sms_ftp) AS subtotal
									FROM tdx_sms_stat WHERE 1=1 $filter";
			$field = "Count(T.phone)*4000 As subtotal";
			if ($web == "GN mkh198542776") $field = "Round(Count(T.phone)*0.7,0)*4000 As subtotal";
			$sqlThisMonthEarning = "SELECT DATE_FORMAT(T.receive_date, '%d-%m-%Y') AS earn_date, $field
FROM `tdx_sms_mo` T
WHERE  1=1 $filmonth
Group by earn_date, telco, service_num, syntax, message_request
Order By id, service_num, telco";
			$sqlThisMonthEarning = "SELECT SUM(P.subtotal) AS subtotal
									FROM (" . $sqlThisMonthEarning .") P";
			$monthQuery = $this->db->query($sqlThisMonthEarning);
			$monthData = $monthQuery->row();
			$total = intval($monthData->subtotal);		
			
			$data['earn_this_month'] = $total;			
			$data['num_this_month'] = $total / 4000;			
			$data['currentUser'] = $this->session->userdata('website_user');
			$this->layout->view(THEME_DIR.'/website_sms_index', $data);
		}
	}
}